<?php
/**
 * Created by veema.
 * Function:
 * Date: 20/9/15
 * Time: 17:45
 */

namespace app\api\controller;


use think\Controller;
use app\common\service\Exception;
use app\common\service\QueueService;
use think\Db;
use think\Error;
use think\Log;

class Initmatch extends Controller
{
    /**
     * 将某个比赛全部比赛初始化
     * @param $match_id 比赛id
     * @return success
     */
    public function initMatch($match_id){
        //************  获取没有初始化过的赛艇赛事 *******//
//        $match = Db::table('up_match')
//            ->where('match_cate', 2) // 赛艇
//            ->where('match_init', 1) // 未初始化过
//            ->where('match_status', 2) //  进行中
//            ->field('match_id')
//            ->find();
        // 如果有，进入初始化
//        if($match){
//            $match_id = $match['match_id'];
//            if(!$match_id){
//                return json(array('status' => 100, 'msg' => "比赛id不能为空"));
//            }
            $match_turns_rounds = Db::table('up__match_turns_rounds')->where('match_id',$match_id)->select();
            if($match_turns_rounds!=[]||count($match_turns_rounds)!=0){
                return json(array('status' => 100, 'msg' => "请勿重复初始化"));
            }
//            Db::startTrans();
                // 原来的逻辑
                $all_data = Db::table('up_match_apply_info')
                    ->where(['ma_match_id'=>$match_id, 'ma_role' => 1])
                    ->distinct('ma_group,ma_match_id,ma_boat_type,ma_distance')
                    ->field('ma_group,ma_match_id,ma_boat_type,ma_distance')
                    ->select();
                foreach ($all_data as $k=>$v){
                    $this->setMatchTurnsInfo($v['ma_group'], $v['ma_match_id'], $v['ma_boat_type'], $v['ma_distance']);
                }
                //初始化所有船颜色
                $all_user = Db::table('up_match_apply_info')->where(['ma_match_id'=>$match_id])->select();
                foreach ($all_user as $key=>$value){
                    $color  = randrgb();
                    Db::table('up_match_apply_info')->where(['ma_id'=>$value['ma_id']])->update(['ma_color'=>$color]);
                }
                // 新增的逻辑,将赛事设置为已经初始化
                Db::table('up_match')->where('match_id', $match_id)->setField('match_init', 2);
//                Db::commit();
//        }
//        return json(array('status' => 200, 'msg' => '初始化成功！'));
    }

    /**
     * 将某个比赛某个组别某个船型某个距离的全部比赛创建初始化
     * @param $ma_group 赛艇组别
     * @param $match_id 比赛id
     * @param $ma_boat_type 船型
     * @param $ma_distance 赛道长度
     * @return success
     */
    public function setMatchTurnsInfo($ma_group, $match_id, $ma_boat_type, $ma_distance)
    {
//        $data['match_id']      = input('match_id');
//        $data['ma_group']      = input('ma_group');
//        $data['ma_boat_type'] = input('$ma_boat_type');
//        $data['ma_distance']   = input('ma_distance');

//        if(!$data['match_id']||!$data['ma_group']||!$data['ma_boat_type']||!$data['ma_distance']){
//            return json(array('status' => 100, 'msg' => '参数不全'));
//        }

        //查询赛道个数
        $track = Db::table('up_match')->where('match_id', $match_id)->value('match_saidao_num');
        if(!$track){
            throw new Exception('赛道数不能为空', 4);
        }
        //查询这一组次比赛参与的人数
        $sign_up_num = Db::table('up_match_apply_info')->where([
            'ma_match_id'  => $match_id,
            'ma_group'     => $ma_group,
            'ma_boat_type' => $ma_boat_type,
            'ma_distance'  => $ma_distance,
        ])->count();
        /*    1、3-4条艇
            a）分组方案：一组初赛，一组决赛
            b)根据初赛成绩排定决赛的航道
            2、5-6条艇
            a）分组方案：两组预赛，一组决赛
            b)按预赛成绩排序，前4名进入决赛
            3、7-8条艇
            a）分组方案：2组预赛，决赛A/B
            b)按预赛成绩排序，前4名进入决赛,其余进入决赛B
            4、9-10条艇
            a）分组方案：三组预赛，决赛A/B
            b)按预赛成绩排序，1-4名进入决赛A,5-8进入决赛B
            5、11条艇以上
            a）分组方案：三组以上预赛，决赛A/B/C
            b)按预赛成绩排序，1-4名进入决赛A,5-8进入决赛B,9-12进决赛C*/
        if ($track == 4) {
            //预赛组数
            $preliminary_num = ceil($sign_up_num / 4);
            //决赛组数
            $final_num = 0;
            if ($sign_up_num % 4 == 0) {
                $final_num = $preliminary_num;
            } else {
                if($sign_up_num % 2 != 0){
                    $final_num = floor($sign_up_num / 6) + 1;
                }else{
                    $final_num = floor($sign_up_num / 4);
                }
        
            }

            //循环插入比赛轮次组次表
            $all_turns_rounds = $final_num + $preliminary_num;
            $data             = [];
            //初赛插入
            $this->addTurnsRounds($preliminary_num, 1, $ma_group, $match_id, $ma_boat_type, $sign_up_num, $ma_distance);
            //决赛插入
            $this->addTurnsRounds($final_num, 5, $ma_group, $match_id, $ma_boat_type, $sign_up_num, $ma_distance);
            //return true;

        } else {
            $all_turns_rounds_list = [];
            if ($sign_up_num == 7) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>2]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>5]);
            } elseif ($sign_up_num>=8&&$sign_up_num <= 10) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>2]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>5]);
            }elseif ($sign_up_num>=11&&$sign_up_num <= 12) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>2]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>5]);
            } elseif ($sign_up_num==13) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>2]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>5]);
            }elseif ($sign_up_num>=14&&$sign_up_num <= 15) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>2]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>5]);
            }elseif ($sign_up_num>=16&&$sign_up_num <= 18) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>2]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>5]);
            }elseif ($sign_up_num>=19&&$sign_up_num <= 20) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>2]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>5]);
            }elseif ($sign_up_num>=21&&$sign_up_num <= 24) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>2]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>5]);
            }elseif ($sign_up_num>=25&&$sign_up_num <= 26) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>5,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>1,'mt_id'=>2]);
                //1/4赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>3]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>4]);
                //决赛
                if($sign_up_num==25){
                    array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>5]);
                }else{
                    array_push($all_turns_rounds_list,['rounds'=>5,'mt_id'=>5]);
                }
            }elseif ($sign_up_num>=27&&$sign_up_num <= 30) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>5,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>2,'mt_id'=>2]);
                //1/4赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>3]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>5,'mt_id'=>5]);
            }elseif ($sign_up_num>=31&&$sign_up_num <= 36) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>6,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>3,'mt_id'=>2]);
                //1/4赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>3]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>6,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>6,'mt_id'=>5]);
            }elseif ($sign_up_num>=37&&$sign_up_num <= 40) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>2]);
                //1/4赛
                array_push($all_turns_rounds_list,['rounds'=>4,'mt_id'=>3]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>7,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>7,'mt_id'=>5]);
            }elseif ($sign_up_num>=41&&$sign_up_num <= 48) {
                //预赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>1]);
                //复赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>2]);
                //1/4赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>3]);
                //半赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>4]);
                //决赛
                array_push($all_turns_rounds_list,['rounds'=>8,'mt_id'=>5]);
            }else{

            }
            //循环插入数据库
            foreach ($all_turns_rounds_list as $k=>$v){
                $this->addTurnsRounds($v['rounds'], $v['mt_id'], $ma_group, $match_id, $ma_boat_type, $sign_up_num, $ma_distance);
            }

        }

    }

    /**
     * 循环添加组次轮次
     * @param $rounds_num 这一轮次有多少组比赛
     * @param $mt_id 轮次id（初赛、复赛、四分之一决赛、半决赛、决赛）
     * @param $ma_group 组别id
     * @param $match_id 比赛id
     * @param $ma_boat_type 船型
     * @param $sign_up_num 这轮次有多少人参加
     * @param $ma_distance 赛道距离
     * @return success
     */
    public function addTurnsRounds($rounds_num, $mt_id, $ma_group, $match_id, $ma_boat_type, $sign_up_num, $ma_distance)
    {
        $data = [];
        for ($i = 1; $i <= $rounds_num; $i++) {
            $data['match_id']     = $match_id;
            $data['mt_id']        = $mt_id;
            $data['mr_id']        = $i;
            $data['ma_group']     = $ma_group;
            $data['ma_boat_type'] = $ma_boat_type;
            $data['ma_distance']  = $ma_distance;
            $data['sign_up_num']  = $sign_up_num;
            $data['type']         = 0;
            //获取轮次组次id
            $turns_rounds_id = Db::table('up__match_turns_rounds')->insertGetId($data);

        }
    }



}